[<<Previous Entry]
[^^Up^^]
[Next Entry>>]
[Menu]
[About The Guide]
open(FILEHANDLE,EXPR)
open(FILEHANDLE)
open FILEHANDLE
Opens the file whose filename is given by EXPR, and
associates it with FILEHANDLE. If FILEHANDLE is an
expression, its value is used as the name of the
real filehandle wanted. If EXPR is omitted, the
scalar variable of the same name as the FILEHANDLE
contains the filename. If the filename begins with
"<" or nothing, the file is opened for input. If
the filename begins with ">", the file is opened for
output. If the filename begins with ">>", the file
is opened for appending. (You can put a '+' in
front of the '>' or '<' to indicate that you want
both read and write access to the file.) If the
filename begins with "|", the filename is inter-
preted as a command to which output is to be piped,
and if the filename ends with a "|", the filename is
interpreted as command which pipes input to us.
(You may not have a command that pipes both in and
out.) Opening '-' opens STDIN and opening '>-'
opens STDOUT. Open returns non-zero upon success,
the undefined value otherwise. If the open involved
a pipe, the return value happens to be the pid of
the subprocess. Examples:
$article = 100;
open article || die "Can't find article $article: $!\n";
while (<article>) {...
open(LOG, '>>/usr/spool/news/twitlog');
# (log is reserved)
open(article, "caesar <$article |");
# decrypt article
open(extract, "|sort >/tmp/Tmp$$");
# $$ is our process#
# process argument list of files along with any includes
foreach $file (@ARGV) {
do process($file, 'fh00'); # no pun intended
}
sub process {
local($filename, $input) = @_;
$input++; # this is a string increment
unless (open($input, $filename)) {
print STDERR "Can't open $filename: $!\n";
return;
}
while (<$input>) { # note use of indirection
if (/^#include "(.*)"/) {
do process($1, $input);
next;
}
... # whatever
}
}
You may also, in the Bourne shell tradition, specify
an EXPR beginning with ">&", in which case the rest
of the string is interpreted as the name of a
filehandle (or file descriptor, if numeric) which is
to be duped and opened. You may use & after >, >>,
<, +>, +>> and +<. The mode you specify should
match the mode of the original filehandle. Here is
a script that saves, redirects, and restores STDOUT
and STDERR:
#!/usr/bin/perl
open(SAVEOUT, ">&STDOUT");
open(SAVEERR, ">&STDERR");
open(STDOUT, ">foo.out") || die "Can't redirect stdout";
open(STDERR, ">&STDOUT") || die "Can't dup stdout";
select(STDERR); $| = 1; # make unbuffered
select(STDOUT); $| = 1; # make unbuffered
print STDOUT "stdout 1\n"; # this works for
print STDERR "stderr 1\n"; # subprocesses too
close(STDOUT);
close(STDERR);
open(STDOUT, ">&SAVEOUT");
open(STDERR, ">&SAVEERR");
print STDOUT "stdout 2\n";
print STDERR "stderr 2\n";
Explicitly closing any piped filehandle causes the
parent process to wait for the child to finish, and
returns the status value in $?. Note: on any opera-
tion which may do a fork, unflushed buffers remain
unflushed in both processes, which means you may
need to set $| to avoid duplicate output.
On MS-DOS, "pipes" are actually files, and the sub-
process is run to completion. For output pipes, the
subprocess is run when perl exits or when the pipe
is explicitly closed. For input pipes, the subpro-
gram is run to completion when the open is done.
On MS-DOS, it is recommended that forward slashes be
used to delimit path components in file names. (See
the section on MS-DOS.)
See Also:
close
dbmopen
dbmclose
This page created by ng2html v1.05, the Norton guide to HTML conversion utility.
Written by Dave Pearson